package ws.jdbc.concurrent.driver.sqlserver.tds.datastreamtypes;

import java.nio.ByteBuffer;
import ws.utils.ArrayUtil;

/**
 *
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
public class B_VARCHAR extends VariableLengthDataStream {

  public B_VARCHAR(Byte length, byte[] data) {
    super(data.length, data);
  }

  public B_VARCHAR(String str) {
    super((byte) (2*  str.length()), ArrayUtil.toLittleEndian(str));
  }

  @Override
  public String getData() {
    return toString();
  }

  @Override
  public String toString() {
    return ArrayUtil.fromLittleEndian(data);
  }

  @Override
  public Byte getLength() {
    return (Byte) length;
  }

  @Override
  public byte[] getBytes() {
    return ByteBuffer.allocate(1 + data.length).put((Byte) length).put(data).array();
  }

  @Override
  public int size() {
    return 1 + data.length;
  }
}
